#include "subWidget.h"

subWidget::subWidget(QWidget *parent) : QWidget(parent)
{

/****************************** 显示初始化 ************************************/
    //行号提示
    editor->setMarginType(0,QsciScintilla::NumberMargin);//设置编号为0的页边显示行号。
    editor->setMarginLineNumbers(0,true);//对该页边启用行号
    editor->setMarginWidth(0,40);//设置页边宽度

    editor->setCaretLineVisible(true);  //显示选中行号
    editor->setCaretLineBackgroundColor(Qt::cyan);
    editor->setMarginsBackgroundColor(Qt::lightGray);  //显示行号背景颜色


    editor->setAutoIndent(true);  //设置自动缩进
    editor->setIndentationGuides(true); //缩进导航

    editor->setFont(QFont("Consolas", 11));  //设置显示字体
    editor->setUnmatchedBraceForegroundColor(Qt::blue);  //括号颜色
    editor->setBraceMatching(QsciScintilla::SloppyBraceMatch);   //括号匹配

    //界面
    QVBoxLayout *pLayout = new QVBoxLayout(this);
    pLayout->addWidget(editor);
    pLayout->setContentsMargins(0,0,0,0);
/*************************************************************************/

/****************************** 部分框体初始化 *****************************/
    findDlg = new QDialog(this);
    findDlg->setWindowTitle(tr("查找"));
    findEdit = new QLineEdit(findDlg);
    findbtn= new QPushButton(tr("查找下一个"), findDlg);

    replaceDlg = new QDialog;
    replaceDlg->setWindowTitle("替换");
    replaceEdit = new QLineEdit(replaceDlg);
    replacebtn = new QPushButton("下一个", replaceDlg);
    replaceEdit1 = new QLineEdit(replaceDlg);
    replacebtn1 = new QPushButton("替换", replaceDlg);

    QVBoxLayout *findlayout = new QVBoxLayout(findDlg);
    QVBoxLayout *replacelayout = new QVBoxLayout(replaceDlg);
    QHBoxLayout *replacelayout1 = new QHBoxLayout(replaceDlg);
    QHBoxLayout *replacelayout2 = new QHBoxLayout(replaceDlg);


    findlayout->addWidget(findEdit);
    findlayout->addWidget(findbtn);
    replacelayout1->addWidget(replaceEdit);
    replacelayout1->addWidget(replacebtn);
    replacelayout2->addWidget(replaceEdit1);
    replacelayout2->addWidget(replacebtn1);

    replacelayout->addLayout(replacelayout1);
    replacelayout->addLayout(replacelayout2);

    connect(findbtn, &QPushButton::clicked, this, &subWidget::showFindText);
    connect(replacebtn, &QPushButton::clicked, this, &subWidget::showReplaceText);
    connect(replacebtn1, &QPushButton::clicked, this, &subWidget::replaceText);
/*************************************************************************/
}

/******************* function ************************/

void subWidget::NewFile(){
    static int index = 1;
    QString title = QString("Untitled %1[*]").arg(index);
    this->setWindowTitle(title);
    this->showMaximized();
    index++;
    isEdit = true;
    this->setWindowModified(true);
}

void subWidget::OpenFile(){
    // 获取要打开的文本文件路径
    QString filename = QFileDialog::getOpenFileName(this,
               "获取文本文件", ".", "Text(*.txt *.cpp *.h *.java *.py *.html)");
    if(filename.isEmpty()){
        return;
    }
    // 保存文件路径，将文件设置到窗体
    this->filename = filename;
    QFileInfo info(filename);
    QString title = QString("%1[*]").arg(info.fileName());
    this->setWindowTitle(title);
    // 打开默认为已被编辑过了
    isEdit = true;
    this->setWindowModified(true);
    file.setFileName(filename);
    this->setLexer(filename);
    if(!file.open(QIODevice::ReadWrite)){
        QMessageBox::warning(this,"打开失败","打开文件失败");
        return;
    }
    QTextStream stream(&file);
    stream.setCodec(this->codeName.toLatin1());
    this->geteditor()->setText(stream.readAll());
    file.close();
}

int subWidget::SaveFile(){
    if(!isEdit){
        return -1;
    }
    if(this->filename.isEmpty()){
        QString filename = QFileDialog::getSaveFileName(this,
                            "保存提示", ".", "Text(*.txt *.cpp *.h *.java *.py *.html)");
        if(filename.isEmpty()){
            return -1;
        }
        this->filename = filename;
    }
    // 对文件操作
    file.setFileName(this->filename);
    bool ret = file.open(QIODevice::ReadWrite|QIODevice::Text);
    if(!ret){
        QMessageBox::warning(this, "失败提示", "打开文件失败");
        return -1;
    }
    QTextStream stream(&file);
    stream.setCodec(this->codeName.toLatin1());
    stream << this->geteditor()->text();
    stream.flush();
    file.close();
    this->setWindowModified(false);
    isEdit = false;
    QMessageBox::information(this, "成功提示", "保存文件成功");
    return 0;
}

void subWidget::SaveAsFile(){
    bool isEdit = this->isEdit;
    this->isEdit = true;
    QString filename = this->filename;
    this->filename.clear();
    int ret = SaveFile();
    if(ret < 0){
        this->isEdit = isEdit;
        this->filename = filename;
    }
}
void subWidget::setLexer(const QString &str){
    QsciLexer *textLexer;
    if (str.endsWith(".cpp")||str.endsWith(".h")){//创建一个词法分析器
        textLexer = new QsciLexerCPP;
        textLexer->setFont(QFont("Consolas", 11));
    }
    else if (str.endsWith(".java")){
        textLexer = new QsciLexerJava;
        textLexer->setFont(QFont("Comic Sans MS", 11));
    }
    else if (str.endsWith(".py")){
        textLexer = new QsciLexerPython;
        textLexer->setFont(QFont("Cascadia Code", 11));
    }
    else if (str.endsWith(".html")){
        textLexer = new QsciLexerHTML;
        textLexer->setFont(QFont("Trebuchet MS", 11));
    }
    else
        return;


    editor->setLexer(textLexer);//给QsciScintilla设置词法分析器

    //代码提示
    QsciAPIs *apis = new QsciAPIs(textLexer);
    apis->prepare();

    editor->setAutoCompletionSource(QsciScintilla::AcsAll);   //设置源，自动补全所有地方出现的
    editor->setAutoCompletionCaseSensitivity(true);   //设置自动补全大小写敏感
    editor->setAutoCompletionThreshold(1);    //设置每输入1个字符就会出现自动补全的提示

}
void subWidget::setCodeName(const QString codename){
    this->codeName = codename;
}
/******************* end func ************************/
/******************* events **************************/
void subWidget::closeEvent(QCloseEvent *event){
    if(!isEdit){
        return;
    }
    QMessageBox::StandardButton ret = QMessageBox::information(this, "保存提示", "你需要保存吗？",
                             QMessageBox::Yes|QMessageBox::No|QMessageBox::Cancel,
                             QMessageBox::Yes);
    if(ret == QMessageBox::Yes){
        SaveFile();
        event->accept();
    }else if(ret == QMessageBox::No)
        event->accept();
    else
        event->ignore();
}

void subWidget::showFindText(){
    QString str = findEdit->text();
    this->geteditor()->findFirst(str,true,false,true,true);
}

void subWidget::showReplaceText(){
    QString str = replaceEdit->text();
    this->geteditor()->findFirst(str,true,false,true,true);
}
void subWidget::replaceText(){
    QString str = replaceEdit->text();
    QString str1 = replaceEdit1->text();
    this->geteditor()->replace(str1);
    this->geteditor()->findFirst(str,true,false,true,true);
}
/******************* end events *********************/
